package com.xia.qianke.utils;

import com.alibaba.fastjson.JSON;
import com.xia.qianke.dao.CountDAO;
import com.xia.qianke.entity.*;
import com.xia.qianke.entity.client.Client;
import com.xia.qianke.entity.qk.user.Sckd;
import com.xia.qianke.entity.qk.vx.Text;
import com.xia.qianke.entity.qk.vx.VxAccessToken;
import com.xia.qianke.entity.qk.vx.VxRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName HttpUtils
 * @Description TODO
 * @Author XJY
 * @Date 2022/7/6 13:53
 * @Version 1.0
 **/
@Component
public class HttpUtils {

    @Autowired
    RestTemplate restTemplate;
    @Autowired
    DataUtils dataUtils;
    @Autowired
    CountDAO countDAO;

    public QkAccessToken getAccessToken(){
        Map<String,String> params=new HashMap<>();
        params.put("appid","65711");
        params.put("did","7013870");
        params.put("secret","178da87713634e41");
        QkAccessToken accessToken = restTemplate.getForObject ("https://api.saas-work" +
                ".cn/cgi-bin/oauth/access_token?appid" +
                "={appid}&did={did}&secret={secret}&expire=100", QkAccessToken.class, params);
        return accessToken;
    }

    public QkPostData postData(Client client){
        Sckd sckd = dataUtils.changeData (client);
        //数据转化成api需要的json格式
        String s = JSON.toJSONString (sckd);
        //封装json数据到请求体中
        HttpHeaders headers = new HttpHeaders ();
        headers.setContentType (MediaType.APPLICATION_JSON_UTF8);
        HttpEntity<String> httpEntity = new HttpEntity<> (s, headers);
        //请求体
        String accessToken = getAccessToken ().getAccess_token ();
        String url = "https://api.saas-work.cn/cgi-bin/customer/import?access_token={accessToken}";

        ResponseEntity<QkPostData> entity = restTemplate.postForEntity (url, httpEntity,
                QkPostData.class, accessToken);
        QkPostData body = entity.getBody ();
        return body;
    }

    public VxAccessToken getVxAccessToken(){
        Map<String,String> params=new HashMap<>();
        params.put("corpid","ww59ebd824952cec15");
        params.put("corpsecret","Sn1KlZLARtjx49nBNHzetsLKAyDWTyKgfi-Wiv9Efyk");
        String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corpid}" +
                "&corpsecret={corpsecret}";
        VxAccessToken vxAccessToken = restTemplate.getForObject (url, VxAccessToken.class, params);
        return vxAccessToken;
    }

    public void PostVxMsg(){
        SimpleDateFormat sdf = new SimpleDateFormat ("hh:mm:ss");
        String time = sdf.format (new Date ());
        Text text =
                new Text ("渠道：申城快贷"+"\n时间："+time+"\n数量：" + countDAO.getTimes ().toString ());
        VxRequest vxRequest = new VxRequest ("1","text",1000002,text,0);
        String s = JSON.toJSONString (vxRequest);
        //封装json数据到请求体中
        HttpHeaders headers = new HttpHeaders ();
        headers.setContentType (MediaType.APPLICATION_JSON_UTF8);
        HttpEntity<String> httpEntity = new HttpEntity<> (s, headers);
        String vxToken = getVxAccessToken ().getAccess_token ();
        String url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={vxToken}";
        restTemplate.postForEntity (url, httpEntity, String.class, vxToken);
    }
}
